package droid.frame.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.widget.EditText;

@SuppressLint("AppCompatCustomView")
public class CodeEditText extends EditText {

	public CodeEditText(Context context, AttributeSet attrs) {
		super(context, attrs);

	}

	/**
	 * If you handled the event, return true <br>
	 * If you want to allow the event to be handled by the next receiver, return false.
	 */
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 删除的时候
		if (keyCode == KeyEvent.KEYCODE_DEL) {
			ViewGroup parent = (ViewGroup) getParent();
			for (int i = parent.getChildCount() - 1; i >= 0; i--) {
				EditText edit = (EditText) parent.getChildAt(i);
				if (TextUtils.isEmpty(edit.getText()) == false) {
					edit.setText("");
					// 移动光标到最后一个
					if (i > 0) {
						EditText pre = (EditText) parent.getChildAt(i - 1);
						pre.requestFocus();
					} else {// 当前是第一个元素
						edit.requestFocus();
					}
					break;
				}
			}
			return false;
		}
		return super.onKeyDown(keyCode, event);
	}
}
